package com.vjwealth.android.nos.base

import android.content.pm.PackageInfo
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import com.vjwealth.android.nos.R
import com.vjwealth.android.nos.widget.CustomeAlertDialog
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.layout_title.*

open class BaseActivity : AppCompatActivity(), BaseView {

    protected var loadingDialog: AlertDialog? = null
    protected var mLayoutInflater: LayoutInflater? = null
    protected var mShowTitleLayout = true
    protected var mPackageInfo: PackageInfo? = null
    protected var mRootView: View? = null

    /**
     * 获取状态栏高度——方法1
     * 获取status_bar_height资源的ID
     * 根据资源ID获取响应的尺寸值
     **/
    protected val statusHeight: Int
        get() {
            var statusBarHeight1 = -1
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                statusBarHeight1 = resources.getDimensionPixelSize(resourceId)
            }
            return statusBarHeight1
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        loadingDialog = CustomeAlertDialog.instance.getLoadingAlertDialog(this)
        mLayoutInflater = LayoutInflater.from(this)
        mPackageInfo = getPackageManager().getPackageInfo(getPackageName(), 0)
        BaseApplication.instance.getActivityManager().pushActivity(this)
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        if (mShowTitleLayout) {
            mRootView = mLayoutInflater!!.inflate(R.layout.activity_base, null)
            setContentView(mRootView)
            mContentView.addView(mLayoutInflater!!.inflate(layoutResID, null))
            setContentView(mRootView)
            val layoutParams = mStatusView.layoutParams
            layoutParams.height = statusHeight
            mStatusView.layoutParams = layoutParams
            mTitleBackImage.setOnClickListener { finish() }
            mTitleRightImage.setOnClickListener { view -> onTitleMenuClick(view) }
            mTitleRrightView.setOnClickListener { view -> onTitleMenuClick(view) }
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    }

    protected fun onTitleMenuClick(view: View) {

    }

    override fun onDestroy() {
        super.onDestroy()
        BaseApplication.instance.getActivityManager().popActivity(this)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        BaseApplication.instance.getActivityManager().popActivity(this)
    }

    override fun showLoading() {
        loadingDialog!!.show()
    }

    override fun dismissLoading() {
        loadingDialog!!.dismiss()
    }

    override fun showExceptionMessage(message: String) {
        if (TextUtils.isEmpty(message)) {
            Toast.makeText(this, resources.getString(R.string.network_exception), Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(this, message, Toast.LENGTH_LONG).show()
        }
    }

    override fun onPause() {
        super.onPause()
        //umeng统计
//        MobclickAgent.onPause(this)
    }

    override fun onResume() {
        super.onResume()
        //umeng统计
//        MobclickAgent.onResume(this)
    }

//    open override fun attachBaseContext(newBase: Context) {}
}
